/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.asset.inventory.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.asset.inventory.entity.InventoryTask;
import com.fuyao.cloud.asset.inventory.service.InventoryTaskService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务盘点管理表
 *
 * @author lyhills
 * @date 2023-09-11 12:01:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/inventorytask" )
@Api(value = "inventorytask", tags = "任务盘点管理表管理")
public class InventoryTaskController {

    private final  InventoryTaskService inventoryTaskService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param inventoryTask 任务盘点管理表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    @PreAuthorize("@pms.hasPermission('inventory_inventorytask_view')" )
    public R getInventoryTaskPage(Page page, InventoryTask inventoryTask) {
        return R.ok(inventoryTaskService.page(page, Wrappers.query(inventoryTask)));
    }


    /**
     * 通过id查询任务盘点管理表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('inventory_inventorytask_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        InventoryTask inventoryTask = inventoryTaskService.getById(id);
        if(StrUtil.isNotEmpty(inventoryTask.getAssetStatus())){
            inventoryTask.setAssetStatusTemp(StrUtil.split(inventoryTask.getAssetStatus(),","));
        }
        return R.ok(inventoryTask);
    }

    /**
     * 新增任务盘点管理表
     * @param inventoryTask 任务盘点管理表
     * @return R
     */
    @ApiOperation(value = "新增任务盘点管理表", notes = "新增任务盘点管理表")
    @SysLog("新增任务盘点管理表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('inventory_inventorytask_add')" )
    public R save(@RequestBody InventoryTask inventoryTask) {
        boolean save = inventoryTaskService.save(inventoryTask);
        return R.ok(inventoryTask);
    }

    /**
     * 修改任务盘点管理表
     * @param inventoryTask 任务盘点管理表
     * @return R
     */
    @ApiOperation(value = "修改任务盘点管理表", notes = "修改任务盘点管理表")
    @SysLog("修改任务盘点管理表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('inventory_inventorytask_edit')" )
    public R updateById(@RequestBody InventoryTask inventoryTask) {
        return R.ok(inventoryTaskService.updateById(inventoryTask));
    }

    /**
     * 通过id删除任务盘点管理表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除任务盘点管理表", notes = "通过id删除任务盘点管理表")
    @SysLog("通过id删除任务盘点管理表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('inventory_inventorytask_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(inventoryTaskService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param inventoryTask 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('inventory_inventorytask_export')" )
    public List<InventoryTask> export(InventoryTask inventoryTask) {
        return inventoryTaskService.list(Wrappers.query(inventoryTask));
    }

    /**
    * 创建方法
    * @return
    */
    @GetMapping(value = "/create")
    public R create() {
        return R.ok(inventoryTaskService.create());
    }

    /**
    * 批量删除方法
    * @param ids
    * @return R
    */
    @ApiOperation(value = "批量删除", notes = "批量删除通知公告")
    @SysLog("批量删除任务盘点管理表" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('inventory_inventorytask_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return R.ok(inventoryTaskService.removeByIds(ids));
    }
}
